import axios from "axios";
//引入进度条
import nprogress from 'nprogress'
//引入进度条样式
import "nprogress/nprogress.css";
import store from '@/store'

const requests = axios.create({
    baseURL: "/api",
    timeout: 5000,
})

//请求拦截器
requests.interceptors.request.use((config) => {
    // 如果仓库里有uuid，就把他给请求头里
    if (store.state.detail.uuid_token) {
        config.headers.userTempId = store.state.detail.uuid_token
    }
    // 有token，就把token带给服务器
    if (store.state.user.token) {
        config.headers.token = store.state.user.token
    }
    //进度条开始动
    nprogress.start();
    return config;
})

//响应拦截器
requests.interceptors.response.use((res) => {
    //响应成功干点啥
    //进度条结束
    nprogress.done();
    return res.data;
}, (error) => {
    //响应失败干点啥
    return Promise.reject(new Error('failed'));
})

export default requests;